﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using MongoDB.Bson;
using MongoDB.Driver;
using BartonExtension;

namespace DBServiceProvider.MongoDB
{
    public class MongoDBClient
    {
        const string connectionString = "mongodb://localhost";
        private static MongoClient client { get; set; }
        public static bool Init(string connStr)
        {
            if (connStr.isNullOrEmpty())
                connStr = connectionString;
            try
            {
                client = new MongoClient(connStr);
                return true;
            }
            catch(Exception e)
            {
                return false;
            }
        }

        public static MongoCollection<T> GetCollection<T>(string collectionName,string dbname)
        {
            return GetDB(dbname).GetCollection<T>(collectionName);
        }

        private static MongoDatabase GetDB(string dbname)
        {
            if(client != null)
            {
                return client.GetServer().GetDatabase(dbname);
            }
            return null;
        }
    }
}
